#include "vl_thread_looper.h"

using namespace VL;



/**
 * @Author: CimZzz
 * @Date: 2022-09-22 19:19:37
 * 
 * 构造方法
 * 
 * @param looper 线程循环方法
 */
VLThreadLooper::VLThreadLooper(Looper looper): looper(looper), isRunning(true), thread([&]{ this->onThreadLoop(); }) {

}

/**
 * @Author: CimZzz
 * @Date: 2022-09-22 19:20:06
 * 
 * 析构方法
 */
VLThreadLooper::~VLThreadLooper() {
	this->stop();
}

/**
 * @Author: CimZzz
 * @Date: 2022-09-22 19:20:40
 * 
 * 停止线程循环
 */
void VLThreadLooper::stop() {
	this->isRunning = false;
	if(this->thread.joinable()) {
		this->thread.join();
	}
}

/**
 * @Author: CimZzz
 * @Date: 2022-09-22 19:21:27
 * 
 * 执行线程循环方法
 */
void VLThreadLooper::onThreadLoop() {
	while(isRunning) {
		if(!looper()) {
			this->isRunning = false;
		}
	}
}
